package itbour.onetouchshow.activity.userinfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.google.gson.Gson;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.web.CommWebActivity;
import itbour.onetouchshow.adapter.XiuMiRecordAdapter;
import itbour.onetouchshow.base.CustomGifHeader;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.ListDataBean;
import itbour.onetouchshow.bean.UserDetailInfoBean;
import itbour.onetouchshow.constants.AppConst;

/**
 * Created by guoh on 2018/3/27.
 * 功能描述：我的秀米
 * 需要的参数：秀米余额
 */

public class XiuMiDetailActivity extends MVPBaseActivity<XiuMiDetailContract.View, XiuMiDetailPresenter> implements XiuMiDetailContract.View {

    private static final String KEY_USE_ID = "KEY_USE_ID";
    @BindView(R.id.tv_xiumi_account)
    TextView tvXiumiAccount;
    @BindView(R.id.ls_record)
    ListView lsRecord;
    @BindView(R.id.refreshView)
    XRefreshView refreshView;
    private Context mContext;
    private XiuMiRecordAdapter recordAdapter;
    private int userId;

    private int mPageIndex = 0;
    private int totalIndex = 1;

    //当前手势是下拉刷新 还是加载更多
    private boolean reload = true;


    public static Intent getLaunchIntent(Context context, int userId) {
        Intent intent = new Intent(context, XiuMiDetailActivity.class);
        intent.putExtra(KEY_USE_ID, userId);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_xiumi_detail;
    }

    @Override
    protected void afterCreate(Bundle bundle) {
        mContext = XiuMiDetailActivity.this;
        userId = getIntent().getIntExtra(KEY_USE_ID, -1);
        initRefreshView();
        if (userId != -1) {
            mPresenter.getUserBalance(userId);
            mPresenter.getBalanceRecord(userId, mPageIndex, 12);
        }

        recordAdapter = new XiuMiRecordAdapter(mContext);
        lsRecord.setAdapter(recordAdapter);
    }

    @Override
    public void loadSucceed(String result) {
        ListDataBean results = new Gson().fromJson(result, ListDataBean.class);
        if (results == null || results.list == null || results.list.size() <= 0 && reload) {
            refreshLoadComplete();
            refreshView.setEmptyView(R.layout.view_no_record);
            refreshView.enableEmptyView(true);
        } else {
            if (refreshView.isEmptyViewShowing()) {
                refreshView.enableEmptyView(false);
            }
            this.totalIndex = results.getTotalPage();
            if (reload) {
                //刷新
                refreshView.stopRefresh();
                if (results.list != null) {
                    recordAdapter.replaceAll(results.list);
                }
            } else {
                //加载更多
                refreshView.stopLoadMore();
                if (results.list != null) {
                    recordAdapter.addAll(results.list);
                }
            }
        }

    }


    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("我的秀米");
        getToolBarView().getRightImageView().setVisibility(View.VISIBLE);
        getToolBarView().setRightImageResource(getResources().getDrawable(R.mipmap.icon_question_explain));
        getToolBarView().setRightIocnOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(AppConst.WEBTYPE, AppConst.TYPE_QA);
                openActivity(CommWebActivity.class, bundle);
            }
        });
    }

    @Override
    public void loadFaild(String error) {
        //显示net error
        refreshView.enableEmptyView(true);
        refreshView.setEmptyView(R.layout.view_network_error);
        refreshLoadComplete();
    }


    private void refreshLoadComplete() {
        if (reload) {
            refreshView.stopRefresh();
        } else {
            refreshView.stopLoadMore();
        }
    }

    private void getData(boolean reload) {
        //刷新与加载更多分别进行判断
        this.reload = reload;
        if (reload) {
            mPageIndex = 0;
            mPresenter.getBalanceRecord(userId, mPageIndex, 12);
        } else {
            if (mPageIndex < totalIndex) {
                mPageIndex += 1;
            }
            mPresenter.getBalanceRecord(userId, mPageIndex, 12);
        }
    }


    @Override
    public void loadBalanceSuccess(String result) {
        UserDetailInfoBean.UserInfoBean wallet = new Gson().fromJson(result, UserDetailInfoBean.UserInfoBean.class);
        tvXiumiAccount.setText((Integer.parseInt(wallet.getWallet().getPointBalance()) / 100) + "" + wallet.getWallet().getPointBalanceUnit());

    }


    private void initRefreshView() {
        refreshView.setAutoRefresh(false);
        refreshView.setAutoLoadMore(true);
        refreshView.stopLoadMore(false);
        refreshView.setPullLoadEnable(true);
        refreshView.setMoveForHorizontal(true);
        CustomGifHeader header = new CustomGifHeader(getApplicationContext());
        refreshView.setCustomHeaderView(header);
        refreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(boolean isPullDown) {
                getData(true);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                getData(false);
            }
        });
    }

}
